import Vue from "vue";
import $web3js from "../web3.js";
import $erc20Token from "../erc20Token/index.js";

const contractInfo = require("./dmcMiningContract.json");

let web3 = $web3js.getWeb3();

let contractAddress = "";
let contractConst = {
  contract: web3 ? new web3.eth.Contract(contractInfo.abi, contractAddress) : ""
};

const getDmcMiningAddress = () => {
  return contractAddress;
};

const getDmcMiningContract = () => {
  return contractConst.contract;
};

const getDmcPerBlock = async () => {
  return $web3js.commonCallAndHandler(
    getDmcMiningContract().methods.dmcPerBlock,
    $web3js.fromWei
  );
};

const getTotalAllocPoint = async () => {
  return $web3js.commonCall(getDmcMiningContract().methods.totalAllocPoint);
};

const getCurrentBlockReward = async () => {
  return $web3js.commonCallAndHandler(
    getDmcMiningContract().methods.getCurrentBlockReward,
    $web3js.fromWei
  );
};

const sumPoolAllocPoint = async _pids => {
  return $web3js.commonCallAndHandler(
    getDmcMiningContract().methods.sumPoolAllocPoint,
    handleSumPoolAllocPoint,
    _pids
  );
};

const handleSumPoolAllocPoint = async res => {
  // console.log(res)
  let index = 0;
  let subAllocPoint = res[index++];
  let totalAllocPoint = res[index++];

  subAllocPoint = await $web3js.fromWei(subAllocPoint);
  totalAllocPoint = await $web3js.fromWei(totalAllocPoint);

  return {
    subAllocPoint,
    totalAllocPoint
  };
};

const getDmcMiningPoolInfo = _pid => {
  return $web3js.commonCallAndHandler(
    getDmcMiningContract().methods.getPoolInfo,
    handlePoolInfo,
    _pid
  );
};

const handlePoolInfo = async res => {
  // console.log(res)
  let index = 0;
  let allocPoint = res[index++];
  let totalToken = res[index++];
  let totalReward = res[index++];
  let lpToken = res[index++];

  totalToken = await $erc20Token.fromErc20TokenValue(totalToken, lpToken);
  totalReward = await $web3js.fromWei(totalReward);

  return {
    allocPoint,
    totalToken,
    totalReward,
    lpToken
  };
};

const getDmcMiningUserInfo = (_pid, _user) => {
  return $web3js.commonCallAndHandler(
    getDmcMiningContract().methods.getUserInfo,
    handleUserInfo,
    _pid,
    _user
  );
};

const handleUserInfo = async res => {
  // console.log(res)
  let index = 0;
  let amount = res[index++];
  let reward = res[index++];
  let lpToken = res[index++];

  amount = await $erc20Token.fromErc20TokenValue(amount, lpToken);
  reward = Vue.prototype.$numberUtils.movePointLeft(reward, 18);

  return {
    amount,
    reward,
    lpToken
  };
};

const dmcMiningDeposit = async (_pid, _amount, _contractAddress) => {
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonSend(
    getDmcMiningContract().methods.deposit,
    _pid,
    _amount
  );
};

const dmcMiningDepositGas = async (_pid, _amount, _contractAddress) => {
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonEstimateGas(
    getDmcMiningContract().methods.deposit,
    _pid,
    _amount
  );
};

const dmcMiningWithdraw = async (_pid, _amount, _contractAddress) => {
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonSend(
    getDmcMiningContract().methods.withdraw,
    _pid,
    _amount
  );
};

const dmcMiningWithdrawGas = async (_pid, _amount, _contractAddress) => {
  _amount = await $erc20Token.toErc20TokenValue(_amount, _contractAddress);
  return $web3js.commonEstimateGas(
    getDmcMiningContract().methods.withdraw,
    _pid,
    _amount
  );
};

const dmcMiningWithdrawAll = async _pid => {
  return $web3js.commonSend(getDmcMiningContract().methods.withdrawAll, _pid);
};

const dmcMiningWithdrawAllGas = async _pid => {
  return $web3js.commonEstimateGas(
    getDmcMiningContract().methods.withdrawAll,
    _pid
  );
};

export default {
  getDmcMiningAddress,
  getDmcMiningContract,
  getDmcPerBlock,
  sumPoolAllocPoint,
  getTotalAllocPoint,
  getCurrentBlockReward,
  getDmcMiningPoolInfo,
  getDmcMiningUserInfo,
  dmcMiningDeposit,
  dmcMiningDepositGas,
  dmcMiningWithdraw,
  dmcMiningWithdrawGas,
  dmcMiningWithdrawAll,
  dmcMiningWithdrawAllGas
};
